<?php
declare (strict_types=1);

namespace app\common\tool\Refund\Driver;

use app\common\model\Logs;

class AliPay
{
    public static function create($param, $trade_type)
    {
        $route = 'logs/alipayRefund.log';
        try {
            $config = config('pay')['alipay'];

            $pay = \AliPay\App::instance($config);

            // 参考链接：https://docs.open.alipay.com/api_1/alipay.trade.refund
            $result = $pay->refund($param->out_trade_no, $param->refund_fee);

            Logs::write($route, "支付宝退款单号:{$param->out_trade_no}退款同步响应结果：".json_encode($result,JSON_UNESCAPED_UNICODE));
            //支付宝退款单号:T20230520083055475951920退款同步响应结果：{"code":"10000","msg":"Success","buyer_logon_id":"137***@qq.com","buyer_user_id":"2088112600266631","fund_change":"Y","gmt_refund_pay":"2023-05-25 11:42:04","out_trade_no":"T20230520083055475951920","refund_fee":"0.01","send_back_fee":"0.00","trade_no":"2023052022001466631452414834"}

            if($result['code'] == '10000' && $result['msg'] == 'Success') {
                return ['code'=>1, 'msg'=>'退款成功', 'result'=>$result];
            }else{
                return ['code'=>-1, 'msg'=>'退款失败'];
            }
        } catch(\Throwable $t){
            $errMsg = [
                'desc'=>'退款异常',
                'errMessage'=>$t->getMessage(),
                'errFile'=>$t->getFile(),
                'errLine'=>$t->getLine()
            ];
            Logs::write($route, json_encode($errMsg,JSON_UNESCAPED_UNICODE));
            return ['code'=>-1, 'msg'=>'退款异常'];
        }
    }

}